﻿using System;
using System.Configuration;
using System.Linq;
using System.Web.UI.WebControls;
using Ingebet.Model;
using Ingebet.Model.Domain;
using Ingebet.Utils;

namespace Ingebet.Web
{
    public partial class AccueilCompte : BasePage
    {
        protected void Page_Load(object sender , EventArgs e)
        {
            if (!IsPostBack)
            {
                var util = Global.Container.Resolve<UtilisateurModel>().GetByID(this.UtilID);

                if (util != null)
                {
                    this._lblBienvenue.Text += util.Nom;
                }
            }
            RefreshGrid();
            InialiserInformationPerso();
        }

        private void RefreshGrid()
        {
            var lstResult = Global.Container.Resolve<PariModel>().GetVuePariAvecResultByUtilID(UtilID);
            this.GridResultat.DataSource = lstResult;
            this.GridResultat.DataBind();

            var lstRenc = Global.Container.Resolve<RencontreModel>().GetAllVueRencontre();
            this.GridRencontres.DataSource = lstRenc;
            this.GridRencontres.DataBind();

            var lstPari = Global.Container.Resolve<PariModel>().GetVuePariByUtilId(UtilID);
            this.GridParisUtilisateur.DataSource = lstPari;
            this.GridParisUtilisateur.DataBind();
        }

        private void InialiserInformationPerso()
        {
            var util = Global.Container.Resolve<UtilisateurModel>().GetByID(this.UtilID);

            this.txtNom.Text = util.Nom;
            this.txtPrenom.Text = util.Prenom;
            this.txtEmail.Text = util.Email;
            this.txtPass.Text = util.Password;
            this.txtNbrPoint.Text = util.Solde.ToString();
            this.txtNbrPoint.Enabled = false;
            this.txtEmail.Enabled = false;
            this.txtNom.Enabled = false;
            this.txtPass.Enabled = false;
            this.txtPrenom.Enabled = false;
        }

        protected void Menu_MenuItemClick(object sender , System.Web.UI.WebControls.MenuEventArgs e)
        {
            this.MultiView.ActiveViewIndex = Int32.Parse(e.Item.Value);
        }

        protected void GridRencontres_ItemCommand(object source , DataGridCommandEventArgs e)
        {
            int index = e.Item.ItemIndex;

            int renID = Convert.ToInt32(this.GridRencontres.Items[index].Cells[0].Text);

            if (IsPariExist(UtilID , renID))
            {
                this.lblError.Text = "Pari deja effectué";
            }
            else
            {
                if (e.CommandName == "select")
                {
                    Session["RencontreID"] = renID.ToString();

                    Page.ClientScript.RegisterClientScriptBlock(GetType() ,
                                                "Parier Match" ,
                                                string.Format("window.showModalDialog('{0}','','menubar=no, center=yes, dialogtop=top, status=no, scrollbars=no, width=800,height=500');" ,
                                                                "Parier.aspx") ,
                                                true);
                }
            }
        }

        private bool IsPariExist(int utilID , int renID)
        {
            return Global.Container.Resolve<PariModel>().GetPariByUtilIdRencontreID(utilID , renID);
        }

        protected void btnEnregistrer_Click(object sender , EventArgs e)
        {
            var util = Global.Container.Resolve<UtilisateurModel>().GetByID(UtilID);
            util.Nom = this.txtNom.Text;
            util.Prenom = this.txtPrenom.Text;
            util.Password = this.txtPass.Text;
            Global.Container.Resolve<UtilisateurModel>().Update(util);
            this.btnEnregistrer.Visible = false;
            this.btnModifier.Visible = true;
            InialiserInformationPerso();
        }

        protected void btnModifier_Click(object sender , EventArgs e)
        {
            this.btnModifier.Visible = false;
            this.txtNom.Enabled = true;
            this.txtPass.Enabled = true;
            this.txtPrenom.Enabled = true;
            this.btnEnregistrer.Visible = true;
        }
    }
}